home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d21 / dvglue10.arc / TVTNEW.C < prev    next >
C/C++ Source or Header  |  1988-08-13  |  5KB  |  153 lines

  1. /*================================================*/
  2. /* TVTNEW.C                                       */
  3. /*                                                */
  4. /*  Requires MASM or A86 to recompile             */
  5. /*                                                */
  6. /* (c) Copyright 1988 Ralf Brown                  */
  7. /*     All Rights Reserved                        */
  8. /* May be freely copied for noncommercial use as  */
  9. /* long as this copyright notice is kept intact   */
  10. /* and any changes are indicated in the comment   */
  11. /* blocks for the functions                       */
  12. /*================================================*/
  13.  
  14. #pragma inline
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "tvapi.h"
  19.  
  20. /*================================================*/
  21.  
  22. static int task_busy = 0 ;
  23. static void (*start_addr)(int) ;
  24. static char far *user_stack ;
  25. static int start_row, start_col ;
  26. static int free_stack ;
  27. static int stack_size ;
  28. static int switchmenu ;
  29.  
  30. /*================================================*/
  31. /* task_start  initial startup code for task      */
  32. /*   Ralf Brown 5/6/88                            */
  33. /*   Ralf Brown 6/3/88 move initial win pos'ning  */
  34. /*                     into here                  */
  35. /*================================================*/
  36.  
  37. static void far task_start(void)
  38. {
  39.    void (*startaddr)(int) ;
  40.    int parent ;
  41.    char far *stack ;
  42.    int freestack ;
  43.  
  44.    asm mov ax,DGROUP       /* restore DS to proper segment */
  45.    asm mov ds,ax
  46.    asm cli                 /* can't have interrupts when switching stacks */
  47.    asm mov ss,word ptr user_stack+2
  48.    asm mov sp,word ptr user_stack
  49.    asm sti                 /* interrupts OK now */
  50.    asm mov ax,sp           /* store stack pointer for later use */
  51.    asm mov bp,sp           /* set up stack frame */
  52.    asm sub sp,12           /* up to 4 bytes for "startaddr", 8 bytes for others */
  53.    asm mov parent,dx       /* store the segment of parent's task object */
  54.    asm sub ax,word ptr stack_size   /* calculate start of stack area */
  55.    asm mov stack+2,ss
  56.    asm mov stack,ax
  57.  
  58.    startaddr = start_addr ;
  59.    freestack = free_stack ;
  60.    TVwin_move(NIL,start_row,start_col) ;  /* move the window to the desired pos */
  61.    if (switchmenu)         /* if the child does the win_top, it is put on the */
  62.       {
  63.       TVwin_top(NIL) ;     /* Switch Windows menu */
  64.       TVwin_redraw(NIL) ;
  65.       }
  66.  
  67.    /* now we can let the parent task create yet another task, since we're */
  68.    /* done with the static variables */
  69.    task_busy = 0 ;
  70.  
  71.    (*startaddr)(parent) ;  /* jump to real start of task */
  72.    TVostack() ;            /* started on private stack, have to switch back */
  73.    if (freestack)
  74.       free((char *)stack) ;
  75.    TVtask_free(NIL) ;       /* task is done, so kill it */
  76. }
  77.  
  78. /*================================================*/
  79. /* TVnew_task  create a new task                  */
  80. /*   Ralf Brown 5/2/88                            */
  81. /*   Ralf Brown 6/3/88 add parms for init win pos */
  82. /*   Ralf Brown 7/23/88 add switch_menu parameter */
  83. /*================================================*/
  84.  
  85. OBJECT pascal TVtask_new(OBJECT parent,char *title,int row,int col,int rows,int cols,char *stack,int stacksize,void (*startaddr)(int),int switch_menu)
  86. {
  87.    PARMLIST8 p ;
  88.    char old_title[32] ;
  89.    int i ;
  90.  
  91. busy_wait:
  92.    asm mov al,1
  93.    asm lock xchg al,task_busy
  94.    asm or al,al
  95.    asm jz busy_done
  96.    TVpause() ;
  97.    asm jmp busy_wait
  98. busy_done:
  99.  
  100.    start_addr = startaddr ;
  101.    start_row = row ;
  102.    start_col = col ;
  103.    switchmenu = switch_menu ;
  104.    if (stack == NULL)
  105.       {
  106.       if (stacksize < 256)
  107.          stacksize = 256 ;
  108.       if ((stack = malloc(stacksize)) == NULL)
  109.          return NIL ;  /* can't get stack, so can't run */
  110.       stack = stack + stacksize ;
  111.       free_stack = TRUE ;
  112.       stack_size = stacksize ;
  113.       }
  114.    else
  115.       free_stack = FALSE ;
  116.    user_stack = (char far *) stack ;
  117.    p.num_args = 8 ;
  118.    if (title)
  119.       {
  120.       p.arg[0] = (DWORD)(char far *)title ;
  121.       p.arg[1] = (DWORD) strlen(title) ;
  122.       }
  123.    else
  124.       {
  125.       TVqry_title(parent,old_title,sizeof(old_title)) ;
  126.       p.arg[0] = (DWORD)(char far *)old_title ;
  127.       for (i = sizeof(old_title)-1 ; i > 0 && old_title[i] == ' ' ; i--)
  128.          ;
  129.       p.arg[1] = (DWORD) i ;
  130.       }
  131.    if (rows < 0)
  132.       TVqry_size(parent,&rows,&i) ;
  133.    if (cols < 0)
  134.       TVqry_size(parent,&i,&cols) ;
  135.    p.arg[2] = (DWORD) rows ;
  136.    p.arg[3] = (DWORD) cols ;
  137.    p.arg[4] = (DWORD) 0 ;  /* no additional allocation */
  138.    p.arg[5] = (DWORD) -1 ; /* use default task stack size */
  139.    p.arg[6] = (DWORD) 0 ;
  140.    p.arg[7] = (DWORD) (void far (*)(void)) task_start ;
  141.    TVsendmsg(NEW_MSG, parent?TOS:ME, parent, (PARMLIST *)&p) ;
  142.    if (!switch_menu)        /* if the parent does the win_top, the task is not */
  143.       {                     /* put on the Switch Windows menu */
  144.       while (task_busy)
  145.          TVpause() ;        /* give child task a chance to start up */
  146.       TVwin_top((OBJECT) p.arg[0]) ;
  147.       TVwin_redraw((OBJECT) p.arg[0]) ;
  148.       }
  149.    return (OBJECT) p.arg[0] ;
  150. }
  151.  
  152. /* End of TVTNEW.C */
  153.